using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;

namespace AnimalFarm
{
    public partial class Login : Telerik.WinControls.UI.RadForm
    {
        B_Farmer B_farmer = new B_Farmer();
        public Login()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            string userName="", password="";
            string role = drdRole.SelectedText;
            int famerID;
            bool success = false;
            DataTable aTable = null;
            if (role.Equals("Farmer"))
            {
                userName = txtUserName.Text;
                password = txtPassword.Text;
                aTable = B_farmer.GetAllFarmer();
                for (int i = 0; i < aTable.Rows.Count; i++)
                {
                    if (aTable.Rows[i]["Username"].ToString().Equals(userName) &&
                        aTable.Rows[i]["Password"].ToString().Equals(password))
                    {
                        success = true;
                        famerID = int.Parse(aTable.Rows[i]["FarmID"].ToString());
                        MainForm main = new MainForm();
                        main.FarmerID = famerID;
                        this.Hide();
                        main.Show();
                        break;
                    }
                }
                if (!success)
                    MessageBox.Show(this,"Login information incorrect","Login",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}
